package com.xxx.video.main.fragments;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.xxx.video.R;
import com.xxx.video.channel.activitys.ChannelListActivity;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.configs.UserManager;
import com.xxx.video.commen.fragments.BaseFragment;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.ResourcesUtils;
import com.xxx.video.commen.utils.StatusBarUtil;
import com.xxx.video.commen.utils.ViewPager2Utils;
import com.xxx.video.commen.views.recyclerviews.RefreshRecyclerView;
import com.xxx.video.login.activitys.LoginActivity;
import com.xxx.video.main.activitys.HomeHotActivity;
import com.xxx.video.main.activitys.HomeTypeActivity;
import com.xxx.video.main.activitys.MessageActivity;
import com.xxx.video.main.activitys.RecommendActivity;
import com.xxx.video.main.activitys.SearchActivity;
import com.xxx.video.main.activitys.VideoDetailsActivity;
import com.xxx.video.main.adapters.HomeFragmentBannerAdapter;
import com.xxx.video.main.adapters.HomeFragmentRecommendVideoListAdapter;
import com.xxx.video.main.adapters.HomeHotTypeListAdapter;
import com.xxx.video.main.adapters.HomeHotTypeListAdapter2;
import com.xxx.video.main.beans.IndexBean;
import com.xxx.video.main.beans.VideoListBean;
import com.xxx.video.main.presenters.HomeRecommendPresenter;
import com.xxx.video.main.presenters.IHomeRecommendView;
import com.xxx.video.mine.activitys.AccountInformationActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeRecommendFragment extends BaseFragment<HomeRecommendPresenter> implements IHomeRecommendView, RefreshRecyclerView.OnRecyclerViewRefreshOrLoadMoreListener {

    @BindView(R.id.rcv_home_recommend_fragment)
    RefreshRecyclerView rcvHomeRecommendFragment;
    @BindView(R.id.iv_home_fragment_actionbar_left_avatar)
    ImageView ivHomeFragmentActionbarLeftAvatar;
    @BindView(R.id.tv_home_fragment_actionbar_search)
    TextView tvHomeFragmentActionbarSearch;
    @BindView(R.id.iv_home_fragment_actionbar_right_message)
    ImageView ivHomeFragmentActionbarRightMessage;

    @BindView(R.id.tv_home_fragment_actionbar_left_un_login)
    TextView tvHomeFragmentActionbarLeftUnLogin;
    @BindView(R.id.cl_home_fragment_actionbar)
    ConstraintLayout clHomeFragmentActionbar;
    private ConcatAdapter mConcatAdapter;
    /**
     * page
     */
    private int page = 1;
    /**
     * list
     */
    public List<VideoListBean> mAllListBeanList = new ArrayList<>();
    /**
     * handle result list
     */
    public List<VideoListBean> mHandleListBeanList = new ArrayList<>();
    private HomeFragmentRecommendVideoListAdapter mHomeFragmentRecommendVideoListAdapter;
    /**
     * banner adapter
     */
    private HomeFragmentBannerAdapter mHomeFragmentBannerAdapter;

    @Override
    public void initToolbarData() {
        super.initToolbarData();
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) clHomeFragmentActionbar.getLayoutParams();
        layoutParams.setMargins(0, StatusBarUtil.getInstance().getStatusBarHeight(Objects.requireNonNull(getActivity())), 0, 0);
        clHomeFragmentActionbar.setLayoutParams(layoutParams);
    }

    public static HomeRecommendFragment newInstance(Bundle args) {
        HomeRecommendFragment fragment = new HomeRecommendFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected HomeRecommendPresenter createPresenter() {
        return new HomeRecommendPresenter(Objects.requireNonNull(getActivity()), this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_home_recommend;
    }

    @Override
    public void initData(Bundle extras) {
        initList();
        getPresenter().getIndex(page);
    }

    @Override
    public void refreshData(Bundle extras) {

    }
//
//    @Override
//    public void onResume() {
//        super.onResume();
//        if (isFirst) {
//            isFirst = false;
//
//        }
//    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    /**
     * init list
     */
    @Override
    public void initList() {
        rcvHomeRecommendFragment.setLayoutManager(new LinearLayoutManager(getActivity()));
        mConcatAdapter = new ConcatAdapter();
        rcvHomeRecommendFragment.setAdapter(mConcatAdapter);
    }

    @Override
    public void initBanner(List<IndexBean.CarouselListBean> carouselList) {
        if (mHomeFragmentBannerAdapter == null) {
            mHomeFragmentBannerAdapter = new HomeFragmentBannerAdapter(Objects.requireNonNull(getActivity()));
            mConcatAdapter.addAdapter(mHomeFragmentBannerAdapter);
            HomeHotTypeListAdapter2 homeHotTypeListAdapter = new HomeHotTypeListAdapter2(Objects.requireNonNull(getActivity()));
            homeHotTypeListAdapter.setOnItemClickListener((o, position, type) -> {
                switch (type) {
                    case 2:
                        intent2Activity(RecommendActivity.class);
                        break;
                    case 1:
                        intent2Activity(HomeHotActivity.class);
                        break;
                    case 0:
                        intent2Activity(HomeTypeActivity.class);
                        break;
                }
            });
            mConcatAdapter.addAdapter(homeHotTypeListAdapter);
            rcvHomeRecommendFragment.setOnRecyclerViewRefreshOrLoadMoreListener(this);
            mHomeFragmentBannerAdapter.setOnItemClickListener((o, position, type) -> {
                IndexBean.CarouselListBean carouselListBean = (IndexBean.CarouselListBean) o;
                if (carouselListBean.fType == 1) {
                    startChannelListActivity(carouselListBean.fBussId);
                } else {
                    /*
                     * video details
                     */
                    startAppDetailsActivity(carouselListBean.fBussId);
                }
            });
        }
        mHomeFragmentBannerAdapter.setBannerData(carouselList);
    }

    @Override
    public void initData2View(IndexBean successOb) {
        rcvHomeRecommendFragment.resetState();
        showRootLayoutStatus(1);
        if (page == 1) {
            /*
             * init banner
             */
            initBanner(successOb.carouselList);
        }
        /*
         * add data
         */
        /*
         *init recommend list
         */
        if (successOb.page != null && !ListUtils.isEmpty(successOb.page.list)) {
            if (successOb.page.list.size() < 10) {
                /*
                 * no data
                 */
                rcvHomeRecommendFragment.setNoMoreData();
            }
            if (page == 1) {
                mAllListBeanList.clear();
            }
            /*
             * add data
             */
            mAllListBeanList.addAll(successOb.page.list);
            handleListData();
        } else {
            /*
             * no data
             */
            rcvHomeRecommendFragment.setNoMoreData();
        }
    }

    @Override
    public void startAppDetailsActivity(String videoId) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", videoId);
        intent2Activity(VideoDetailsActivity.class, bundle);
    }

    /**
     * handle list data
     */
    private void handleListData() {

        mHandleListBeanList.clear();
        List<VideoListBean> listBeans = new ArrayList<>();
        for (VideoListBean listBean : mAllListBeanList) {
            if (listBeans.size() == 8) {
                VideoListBean listBean1 = new VideoListBean();
                listBean1.list = listBeans;
                mHandleListBeanList.add(listBean1);
                mHandleListBeanList.add(listBean);
                listBeans = new ArrayList<>();
            } else {
                listBeans.add(listBean);
            }
        }
        if (!ListUtils.isEmpty(listBeans) && listBeans.size() <= 8) {
            VideoListBean listBean1 = new VideoListBean();
            listBean1.list = listBeans;
            mHandleListBeanList.add(listBean1);
        }
        /*
         * result handle list data
         */
        initRecommendList(mHandleListBeanList);
    }

    @Override
    public void initRecommendList(List<VideoListBean> mListBeanList) {
        if (mHomeFragmentRecommendVideoListAdapter == null) {
            mHomeFragmentRecommendVideoListAdapter = new HomeFragmentRecommendVideoListAdapter(Objects.requireNonNull(getActivity()));
            mConcatAdapter.addAdapter(mHomeFragmentRecommendVideoListAdapter);
            mHomeFragmentRecommendVideoListAdapter.setOnItemClickListener((BaseRecycleViewAdapter.OnItemClickListener<VideoListBean>) (listBean, position, type) -> {
                /*
                 * app details
                 */
                startAppDetailsActivity(listBean.fContentId);
            });
        }
        mHomeFragmentRecommendVideoListAdapter.setData(mListBeanList);
    }

    @Override
    public void onRecyclerviewRefresh() {
        page = 1;
        getPresenter().getIndex(page);
    }

    @Override
    public void onRecyclerviewLoadMore() {
        page++;
        getPresenter().getIndex(page);
    }

    @Override
    public void startChannelListActivity(String fParentId) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", fParentId);
        intent2Activity(ChannelListActivity.class, bundle);
    }

    @Override
    public void onResume() {
        super.onResume();
        initPreview();
    }
    @Override
    public void startMessageActivity() {
        if (!UserManager.getInstance().isLogin()) {
            startLoginActivity();
            return;
        }
        intent2Activity(MessageActivity.class);
    }

    @Override
    public void startLoginActivity() {
        intent2Activity(LoginActivity.class);
    }

    @Override
    public void startSearchActivity() {
        intent2Activity(SearchActivity.class);
    }

    @Override
    public void startAccountInformationActivity() {
        intent2Activity(AccountInformationActivity.class);
    }

    @OnClick({R.id.tv_home_fragment_actionbar_left_un_login,
            R.id.iv_home_fragment_actionbar_left_avatar, R.id.tv_home_fragment_actionbar_search,
            R.id.iv_home_fragment_actionbar_right_message})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_home_fragment_actionbar_left_un_login:
                startLoginActivity();
                break;
            case R.id.iv_home_fragment_actionbar_left_avatar:
                startAccountInformationActivity();
                break;
            case R.id.tv_home_fragment_actionbar_search:
                startSearchActivity();
                break;
            case R.id.iv_home_fragment_actionbar_right_message:
                startMessageActivity();
                break;
            default:
                break;
        }
    }
    @Override
    public void initPreview() {
        if (UserManager.getInstance().isLogin()) {
            ivHomeFragmentActionbarLeftAvatar.setVisibility(View.VISIBLE);
            tvHomeFragmentActionbarLeftUnLogin.setVisibility(View.GONE);
            Glide.with(this).load(ResourcesUtils.getInstance().getImagePath(UserManager.getInstance().getUserAvatar())).apply(new RequestOptions().circleCrop()
                    .placeholder(R.drawable.icon_default_avatar)
                    .error(R.drawable.icon_default_avatar)
            ).into(ivHomeFragmentActionbarLeftAvatar);
        } else {
            ivHomeFragmentActionbarLeftAvatar.setVisibility(View.INVISIBLE);
            tvHomeFragmentActionbarLeftUnLogin.setVisibility(View.VISIBLE);
        }

    }
}
